<?php

namespace App\Repository\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

/**
 * Class User.
 *
 * @package namespace App\Repository\Models;
 */
class User extends Model implements Transformable,AuthenticatableContract
{
    use TransformableTrait;
    use Authenticatable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'user';

    protected $appends = ['identification_type_name'];

    protected $fillable = ['id','shop_id','name','phone','pic','pwd','pay_pwd','sex','brithday','province','city','area','address','point','money','growth','email','status','identification_type','created_at','updated_at','invitation_code','inviter_id','cp_inviter_id','is_city_partner','lottery_num','is_test_user','deleted_at','personal_enterprise'];

    //认证状态
    public static $identification_type_map = [
        0 => '普通账户',
        1 => '提交认证',
        2 => '认证成功',
        3 => '认证失败',
    ];

    public function getIdentificationTypeNameAttribute()
    {
        if(isset($this->attributes['identification_type'])){
            return array_get(self::$identification_type_map, $this->attributes['identification_type']) ? : '-';
        }
        return null;
    }


    //认证信息
    public  function company(){
        return $this->hasOne(Company::class, 'user_id', 'id');
    }

    //公司信息
    public  function company_material(){
        return $this->hasOne(CompanyMaterial::class, 'user_id', 'id');
    }


    public function carts(){
        return $this->hasMany(Carts::class, 'user_id', 'id');
    }

    public function orders(){
        return $this->hasMany(Orders::class, 'user_id', 'id');
    }

    public function userInvoice(){
        return $this->hasMany(UserInvoice::class, 'user_id', 'id');
    }

    public function address(){
        return $this->hasMany(Address::class, 'user_id', 'id')->with(['regions']);
    }

}
